package com.foreveross.mofang.datapersistence.application.impl;

import javax.inject.Named;

import org.dayatang.querychannel.Page;

import com.foreveross.mofang.datapersistence.application.RDBMSCrawlApplication;
import com.foreveross.mofang.datapersistence.application.vo.RDBMSDataVo;
import com.foreveross.mofang.datapersistence.core.CrawlEntity;

/**
 * 航空采集结点关系性数据库操作基础层接口实现
 * 
 * @author fb
 */
@Named
public class RDBMSCrawlApplicationImpl extends RDBMSBaseImpl<CrawlEntity> implements
		RDBMSCrawlApplication {

	public Page<RDBMSDataVo> pageQuery(int currentPage, int pageSize) {
		return pageQuery(currentPage, pageSize, "select re from CrawlEntity re order by re.id desc");
	}
	
	public RDBMSDataVo getCrow(String rowKey) {
		CrawlEntity ce = CrawlEntity.getCraw(rowKey);
		
		if (ce == null) {
			return null;
		}
		
		return domainToVo(ce);
	}

	public void deleteCrow(String... rowKeys) {
		CrawlEntity.removes(rowKeys);
	}

	public void updateCrow(String rowKey, RDBMSDataVo vo) {
		CrawlEntity ce = CrawlEntity.getCraw(rowKey);
		
		if (ce == null) {
			throw new RuntimeException("rowKey\"" + rowKey + "\"不存在");
		}
		
		ce.setContent(vo.getContent() == null ? ce.getContent() : vo.getContent());
		ce.setRowKey(vo.getRowKey() == null ? ce.getRowKey() : vo.getRowKey());
		ce.save();
	}

}
